package com.ice.body.hardware.controller;
import com.ice.body.business.model.WareHouseModel;
import com.ice.body.business.service.WareHouseService;
import com.ice.body.hardware.model.DeviceQr;
import com.ice.body.hardware.service.DeviceQrService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import com.ice.base.controller.BaseController;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;

import com.ice.base.result.ResponseResult;
import com.ice.base.result.AjaxResult;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2021-12-22
 * Time: 02:57
 */
@RestController
@ApiIgnore(value = "二维码扫描器")
public class DeviceQrController extends BaseController{
    //---------------------------二维码扫描器-----------------------start
    @Resource
    private DeviceQrService deviceQrService;
    @Resource
    private WareHouseService wareHouseService;

    @ApiOperation(value = "二维码扫描器-新增")
    @PostMapping("/api/DeviceQr/insert")
    public AjaxResult<DeviceQr> insert_DeviceQr(DeviceQr model) {
        AjaxResult r = ResponseResult.success();
        try{
            WareHouseModel wareHouseModel = wareHouseService.selectById(model.getWarehouseId());
            model.setWarehouseName(wareHouseModel.getWarehouseName());
            model.setCreateTime(new Date());
            deviceQrService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "二维码扫描器-删除")
    @PostMapping("/api/DeviceQr/deleteById")
    public AjaxResult<DeviceQr> deleteById_DeviceQr(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            Integer count = deviceQrService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "二维码扫描器-更新")
    @PostMapping("/api/DeviceQr/updateById")
    public AjaxResult<DeviceQr> update_DeviceQr(DeviceQr model) {
        AjaxResult r = ResponseResult.success();
        try{
            WareHouseModel wareHouseModel = wareHouseService.selectById(model.getWarehouseId());
            model.setWarehouseName(wareHouseModel.getWarehouseName());
            deviceQrService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "二维码扫描器-查询一个")
    @PostMapping("/api/DeviceQr/selectById")
    public AjaxResult<DeviceQr> selectById_DeviceQr(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           DeviceQr model = deviceQrService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

/*
    @ApiOperation(value = "二维码扫描器-查询多个")
    @PostMapping("/api/DeviceQr/selectAll")
    public Result<DeviceQrModel> selectList_DeviceQr(DeviceQrModel model) {
        Result r = ResponseResult.success();
        try{
           List<DeviceQrModel> list = deviceQrService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
*/

    @ApiOperation(value = "二维码扫描器-查询分页")
    @PostMapping("/api/DeviceQr/selectListByPageHelper")
    public AjaxResult<DeviceQr> selectListByPageHelper_DeviceQr(@RequestParam(name = "pageSize") Integer pageSize,
                                                                @RequestParam(name = "pageNum") Integer pageNum,
                                                                DeviceQr model){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           list = deviceQrService.selectListByModel(model);
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------二维码扫描器-----------------------end
}
